package com.topnav.mc.sse;

import com.js.lib.cache.SysCache;
import com.js.lib.result.Result;
import com.js.lib.utils.JsonUtil;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.common.Constant;
import com.topnav.mc.uitls.PermissionGradeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: kenny
 * @Date: 2023/11/8 13:40
 */
@Service
@Slf4j
public class SseEmitterServiceImpl implements SseEmitterService{
    @Autowired
    private SysCache sysCache;
    @Resource
    private PermissionGradeUtil permissionGradeUtil;

    @Async("taskExecutor")
    @Override
    public void send(MessageBean message) {
        Map<Object,Object> emitterMap =  sysCache.hmget(Constants.CAHCE_SSE_CLIENT);
        //String json = JsonUtil.toJson(result);
        //log.debug(json);
        if (emitterMap != null){
            for (Iterator<Map.Entry<Object, Object>> it = emitterMap.entrySet().iterator(); it.hasNext();) {
                Map.Entry<Object,Object> item = it.next();
                EmitterBean emitterBean = (EmitterBean)item.getValue();
                //权限判断
                PermissionGrade permissionGrade = permissionGradeUtil.getPmGrade(emitterBean.getToken());
                if (permissionGrade == null){
                    continue;
                }
                try {
                    //分权限
                    if (permissionGrade.getPermissionLevel() == 1){
                        if (permissionGrade.getUserId().equals(message.getUserId())){
                            emitterBean.getEmitter().send(Result.ok(message.getMessage()));
                        }
                    }else if (permissionGrade.getPermissionLevel() == 2){
                        if (permissionGrade.getOrgId().equals(message.getOrgId())){
                            emitterBean.getEmitter().send(Result.ok(message.getMessage()));
                        }
                    }else if (permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4 ) {
                        if (message.getOrgPath().startsWith(permissionGrade.getOrgPath())) {
                            emitterBean.getEmitter().send(Result.ok(message.getMessage()));
                        }
                    }else{
                        emitterBean.getEmitter().send(Result.ok(message.getMessage()));

                    }
                }catch (IOException | IllegalStateException e){
                    log.error(">>>>>>> 消息推送给 【{}】 失败",permissionGrade.getUserName());
                    continue;
                }
                log.error(">>>>>>> 消息成功推送给 【{}】,客户端ID：{}",permissionGrade.getUserName(),item.getKey());
            }
        }
    }
}
